<?php
/**
 * Created by PhpStorm.
 * User: yulin
 * Date: 2018-7-10
 * Time: 14:32
 */
namespace app\open\model;

use app\open\filter\CarSellList;
use app\base\exception\RecordNotFoundException;
use think\Db;
use traits\model\SoftDelete;
use app\open\model;
class CarAuction extends Common
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    /**
     * 拍卖列表
     * @param string $fields
     * @param array $where
     * @param int $page
     * @param int $num
     * @return array
     */
    public function sellList($fields = '*', $where = [], $page = 1, $num = 10)
    {
        $filter = new CarSellList();
        $filter->analyzeField();
//        list(, $msg) = $filter->checkField($fields);
        if ($where['car_auction_id']) {
            $where2['car_auction_id'] = $where['car_auction_id'];
        }
        if ($where['company_name']) {
            $where2['company_name'] = $where['company_name'];
        }
        if ($where['number']) {
            $where2['number'] = $where['number'];
        }
        if ($where['state']) {
            $where2['state'] = $where['state'];
        }
        list(, $whereMsg) = $filter->checkWhere($where2);
        $whereMsg['a.delete_time'] = ["null"];

        $buildQuery = function ($query) use ($whereMsg) {
            self::authCompanyQuery($query, $whereMsg, 'a.company_id');
        };

//        $data['data'] = Db::name('car_auction')
            $data['data'] = (new model\CarAuction())
            ->alias('a')
            ->join('car c', 'a.car_id = c.car_id', 'left')
            ->join('car_brand d', 'c.car_brand_id = d.car_brand_id', 'left')
            ->join('car_brand e', 'c.car_series_id = e.car_brand_id', 'left')
            ->join('car_brand f', 'c.car_model_id = f.car_brand_id', 'left')
            ->join('car_type h', 'c.car_type_id = h.car_type_id', 'left')
            ->join('company i', 'a.company_id = i.company_id', 'left')
            ->field($fields)
            ->where($buildQuery)
            ->order('date desc')
            ->page($page, $num)
            ->select();
//        $data['count'] = count($data['data']);
        $data['description'] = $filter->title;
        $data['fields'] = $filter->fieldTitle;
        return [1, $data];
    }

    /**
     * 新增或者编辑拍卖申请
     * @param $params
     * @return $this|false|int
     * @throws RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function sellEdit($params)
    {
        $data = [];

        $where = [];
        $params['car_auction_id'] && $where['car_auction_id'] = $params['car_auction_id'];

        $update = [];
        $params['why'] && $update['why'] = $params['why'];
        $params['car_id'] && $update['car_id'] = $params['car_id'];
        $params['auction_money'] && $update['auction_money'] = $params['auction_money'];
        $params['provincial_administration_result'] && $update['provincial_administration_result'] = $params['provincial_administration_result'];
        $params['bureau_of_finance_result'] && $update['bureau_of_finance_result'] = $params['bureau_of_finance_result'];
        $params['auction_process'] && $update['auction_process'] = $params['auction_process'];
        $params['provincial_administration_again_result'] && $update['provincial_administration_again_result'] = $params['provincial_administration_again_result'];
        $params['turn_over'] && $update['turn_over'] = $params['turn_over'];
        $params['auction_image'] && $update['auction_image'] = $params['auction_image'];
        $params['bureau_of_finance_file'] && $update['bureau_of_finance_file'] = $params['bureau_of_finance_file'];

        if ($where['car_auction_id']) {

            $car_auction_return = Db::name('car_auction')->where($where)->find();

            //某个局拒绝这个申请
            if($update['provincial_administration_result'] == '9' || $update['bureau_of_finance_result'] == '9'){
                //其中一个局拒绝,状态码=3
                $update['state'] = '3';
                $data['data'] = Db::name('car_auction')->where($where)->update($update);
                $data['count'] = $data['data'];
                return [1,$data];
            }
            //省管局审批
            elseif($update['provincial_administration_result'] == '1' && $car_auction_return['state'] < '3'){
                if($car_auction_return['bureau_of_finance_result'] == '1') {
                    //另一个局已同意,状态码=5
                    $update['state'] = '5';
                }
                else{
                    //只有自己局同意,状态码=2
                    $update['state'] = '2';
                }
                $update['provincial_administration_result_date'] = date("Y-m-d H:i:s");
                $data['data'] = Db::name('car_auction')->where($where)->update($update);
                $data['count'] = $data['data'];
                return [1,$data];
            }
            //财政局审批
            elseif($update['bureau_of_finance_result'] == '1' && $car_auction_return['state'] < '3'){
                if($car_auction_return['provincial_administration_result'] == '1') {
                    //另一个局已同意,状态码=5
                    $update['state'] = '5';
                }
                else{
                    //只有自己局同意,状态码=2
                    $update['state'] = '2';
                }
                $update['bureau_of_finance_result_date'] = date("Y-m-d H:i:s");
                $data['data'] = Db::name('car_auction')->where($where)->update($update);
                $data['count'] = $data['data'];
                return [1,$data];
            }
            //省管局二次确认
            //从需求里取消
//            elseif($update['provincial_administration_again_result'] && $car_auction_return['state'] == '4'){
//                $update['state'] = '5';
//                $update['provincial_administration_again_result_date'] = date("Y-m-d H:i:s");
//                $data['data'] = Db::name('car_auction')->where($where)->update($update);
//                $data['count'] = $data['data'];
//                return [1,$data];
//            }
            //拍卖结果
            elseif($update['turn_over'] || $update['auction_image'] || $update['bureau_of_finance_file'] || $update['auction_money']){
                if($car_auction_return['state'] == '5'){
                    if($update['auction_image']){
                        $update['auction_image_date'] = date("Y-m-d H:i:s");
                    }
                    if($update['bureau_of_finance_file']){
                        $update['bureau_of_finance_file_date'] = date("Y-m-d H:i:s");
                    }
                    $update['state'] = '6';
                    $data['data'] = Db::name('car_auction')->where($where)->update($update);
                    $data['count'] = $data['data'];
                    return [1,$data];
                }

            }

        }
        else if ($update['car_id']) {
            $car_return = Db::name('car')->where('car_id',$update['car_id'])->find();
            $update['auction_no'] = $this->createSn();
            $update['state'] = 1;
            $update['date'] = date("Y-m-d H:i:s");
            $update['company_id'] = $car_return['company_id'];
            $data['data'] = Db::name('car_auction')->insert($update);
            Db::name('car')->where('car_id',$update['car_id'])->update(['state' => '7']);
            $data['count'] = '1';
            return [1, $data];
        }
        else {
            $data = ['where' => $where, 'update' => $update, 'params' => $params];
            return [0, $data];
        }
    }

    /**
     * 删除拍卖申请
     * @param $params
     * @return $this|false|int
     * @throws RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function sellDelete($id = '')
    {
        $data = [];
        $car_auction_return = Db::name('car_auction')->where('car_auction_id', $id)->update(['delete_time' => time()]);
        if($car_auction_return) {
            $car_id = Db::name('car_auction')->where('car_auction_id', $id)->value('car_id');
            if($car_id) {
                $car_return = Db::name('car')->where('car_id', $car_id)->update(['state' => '1']);
            }
            $data['count'] = $car_auction_return;
            return [1, $data];
        }
        else{
            $data['msg'] = '删除失败,查无此car_auction_id';
            return [0, $data];
        }
    }
}